package com.example.shopping.security;

import cn.hutool.json.JSONUtil;
import com.example.shopping.dto.Result;
import com.example.shopping.util.RedisClient;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 功能：
 * 作者：张赛特
 * 日期：2025/11/18 11:56
 */
@Component
public class LogoutStatusSuccessHandler implements LogoutSuccessHandler {
   @Resource
   private RedisClient redisClient;

    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        String token=request.getHeader("token");
        if (StringUtils.hasText(token)){
            String key="login:token:"+token;
            redisClient.del(key);
        }
        response.setCharacterEncoding("utf-8");
        response.setContentType("applocation/json");
        Result result=Result.ok("注销成功");
        String json= JSONUtil.toJsonStr(result);
        response.getWriter().write(json);
    }
}